/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package info.threads;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JPanel;

public class Analoguhr extends javax.swing.JFrame {

    private Thread runner;
    private GregorianCalendar now;
    Image buffer;
    Graphics2D gBuffer;

    Calendar date;

    int nn=170, r=100; // Zentrum und Radius

    public Analoguhr() {
        initComponents();
        runner=new Thread(new clock());
        runner.start();
        buffer=createImage(this.getSize().width, this.getSize().height-20);
        gBuffer=(Graphics2D)buffer.getGraphics();
        
    }

    private class clock implements Runnable{
        public void run(){
            while (true){
                animationPan.repaint();
            }
            
        }
    }
    
    class animationPanel extends JPanel {

        public void paint(Graphics g){
            gBuffer.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

            gBuffer.clearRect(0,0,this.getSize().width, this.getSize().height-20);
            gBuffer.setColor(new Color(0,0,0));
            gBuffer.drawOval(nn-2,nn-2,4,4);

           
            for (int i=0;i<4;i++) {
                gBuffer.drawLine(
                    nn+(int)((r-7)*Math.cos(Math.toRadians(90*i))),
                    nn+(int)((r-7)*Math.sin(Math.toRadians(90*i))),
                    nn+(int)(r*Math.cos(Math.toRadians(90*i))),
                    nn+(int)(r*Math.sin(Math.toRadians(90*i))));
            }
            for (int i=0;i<12;i++) {
                gBuffer.drawLine(
                    nn+(int)((r-5)*Math.cos(Math.toRadians(30*i))),
                    nn+(int)((r-5)*Math.sin(Math.toRadians(30*i))),
                    nn+(int)(r*Math.cos(Math.toRadians(30*i))),
                    nn+(int)(r*Math.sin(Math.toRadians(30*i))));
            }
            for (int i=0;i<60;i++) {
                gBuffer.drawLine(
                    nn+(int)((r-2)*Math.cos(Math.toRadians(6*i))),
                    nn+(int)((r-2)*Math.sin(Math.toRadians(6*i))),
                    nn+(int)(r*Math.cos(Math.toRadians(6*i))),
                    nn+(int)(r*Math.sin(Math.toRadians(6*i))));
            }

            date = new GregorianCalendar();

            int hours = date.get(Calendar.HOUR_OF_DAY);
            int minutes = date.get(Calendar.MINUTE);
            int seconds = date.get(Calendar.SECOND);

            // Stunden
            gBuffer.drawLine(
                nn,nn,nn+(int)(60*Math.cos(Math.toRadians(hours%12*30+minutes/2.0-90))),
                nn+(int)(60*Math.sin(Math.toRadians(hours%12*30+minutes/2.0-90))));

            // Minuten
            gBuffer.drawLine(
                nn,nn,nn+(int)(85*Math.cos(Math.toRadians(minutes*6-90))),
                nn+(int)(85*Math.sin(Math.toRadians(minutes*6-90))));

            // Sekunden
            gBuffer.setColor(new Color(170,170,170));
            gBuffer.drawLine(
                nn,nn,nn+(int)(90*Math.cos(Math.toRadians(seconds*6-90))),
                nn+(int)(90*Math.sin(Math.toRadians(seconds*6-90))));

            g.drawImage (buffer, 0, 0, this);

            try {Thread.sleep(50);}
            catch (InterruptedException e) {}

        }
    
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        animationPan = new animationPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Analoguhr");

        javax.swing.GroupLayout animationPanLayout = new javax.swing.GroupLayout(animationPan);
        animationPan.setLayout(animationPanLayout);
        animationPanLayout.setHorizontalGroup(
            animationPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        animationPanLayout.setVerticalGroup(
            animationPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 295, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)
            .addComponent(animationPan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(animationPan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Analoguhr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Analoguhr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Analoguhr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Analoguhr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Analoguhr().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel animationPan;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}
